package nl.science.uva.pico.operator;

import nl.science.uva.pico.Stack;
import nl.science.uva.pico.operand.Operand;


/** 
 * Concat adds 2 String variables. 
 * 
 * @author Ivo van Dongen, Karel Pieterson
 * @version 1.0
 */
public class Concat extends Operator {
	
    /** 
     * Concatenate (add) two String variables 
     * 
     * @param stack program stack to perform concatenation on
     */
	public Concat(Stack stack) {
		super(stack, null, null);
	}
	
    /** 
     * Concat This instruction concatenates two string constants at the top of the stack.
     *
     * <p>Concat pops 2 values from the top of the stack as Strings, adds them together (concatenation) and pushes the resulting
     * value back on the stack 
     * 
     * @throws IllegalStateException 
     *  
     */
	public void perform() {
		String[] operands = new String[2];
		
		//Check number of operands
		if(getStack().size() >= 2) {
			operands[0] = getStack().pop().getStringValue();
			operands[1] = getStack().pop().getStringValue();
			
			String result = operands[0] + operands[1];
			getStack().push(new Operand(result));
			
		} else {
			throw new IllegalStateException(" Too few operands, Concat needs 2 operands");
		}
	}
}
